/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Ventanas;

import Gestores.Entidades.EntidadPagoMatricula;
import Gestores.Entidades.EntidadPermiso;
import Negocios.PagoMatriculaNegocio;
import Ventana.Interfaces.LogError;

/**
 *
 * @author Lion
 */
public class PagoMatricula extends javax.swing.JFrame implements LogError{

    /**
     * Creates new form PagoMatricula
     */
    private int monto= 350;
    private PagoMatriculaNegocio negocio;
    private EntidadPagoMatricula entidad;
    private EntidadPermiso Permiso;
    
    public PagoMatricula() {
        initComponents();
        this.configurarFrame();
        setTitle("Colegio Economista-Pago Matricula");
        negocio= new PagoMatriculaNegocio();
    }
    
     public PagoMatricula(EntidadPermiso Permiso) {
        this.Permiso = Permiso;
        initComponents();
        setComponentesDisabled();
        this.configurarFrame();
        setTitle("Colegio Economista-Pago Matricula");
        negocio= new PagoMatriculaNegocio();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txt_cod = new javax.swing.JFormattedTextField();
        txt_ci = new javax.swing.JFormattedTextField();
        txt_fecha = new com.toedter.calendar.JDateChooser();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txt_descripcion = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txt_mensaje = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        btn_nuevo = new javax.swing.JButton();
        btn_pagar = new javax.swing.JButton();
        btn_eliminar = new javax.swing.JButton();
        btn_buscar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setLayout(new java.awt.GridLayout(3, 0));
        jPanel1.add(txt_cod);

        txt_ci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txt_ciActionPerformed(evt);
            }
        });
        jPanel1.add(txt_ci);
        jPanel1.add(txt_fecha);

        jLabel1.setText("Codigo");

        jLabel2.setText("C.I");

        jLabel3.setText("Fecha");

        jLabel4.setText("Detalle");

        txt_descripcion.setColumns(20);
        txt_descripcion.setRows(5);
        jScrollPane1.setViewportView(txt_descripcion);

        jLabel5.setText("Mensaje");

        txt_mensaje.setColumns(20);
        txt_mensaje.setRows(5);
        jScrollPane2.setViewportView(txt_mensaje);

        jPanel3.setLayout(new java.awt.GridLayout());

        btn_nuevo.setText("Nuevo");
        btn_nuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_nuevoActionPerformed(evt);
            }
        });
        jPanel3.add(btn_nuevo);

        btn_pagar.setText("Pagar");
        btn_pagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_pagarActionPerformed(evt);
            }
        });
        jPanel3.add(btn_pagar);

        btn_eliminar.setText("Eliminar");
        btn_eliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_eliminarActionPerformed(evt);
            }
        });
        jPanel3.add(btn_eliminar);

        btn_buscar.setText("Buscar");
        btn_buscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_buscarActionPerformed(evt);
            }
        });
        jPanel3.add(btn_buscar);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 313, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel3))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(21, 21, 21)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(43, 43, 43))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_nuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_nuevoActionPerformed
        // TODO add your handling code here:
           this.txt_ci.setText("");
        this.txt_cod.setText("");
        this.txt_descripcion.setText("");
        this.txt_fecha.setDateFormatString("");
        this.txt_mensaje.setText("");
    }//GEN-LAST:event_btn_nuevoActionPerformed

    private void btn_pagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_pagarActionPerformed
        // TODO add your handling code here:
        
     try{
         if(this.txt_ci.getText().isEmpty()){
                mostrarError("Debe introducir el C.I. del interezado");
         }else{
             if(this.txt_descripcion.getText().isEmpty())
                 mostrarError("Debe introducir el C.I. del interezado");
             else{
                 this.negocio.insertarPagoMatricula(this.txt_ci.getText(),
                         this.txt_descripcion.getText());
                 mostrarError("Transaccion realizada con exito");
               }
         }
         
     }catch(Exception e){
         mostrarError(e.getMessage());
     
     }
    }//GEN-LAST:event_btn_pagarActionPerformed

     public void cargarDatos(Object object){
            this.entidad= (EntidadPagoMatricula)object;
            this.txt_ci.setText(String.valueOf(entidad.getCi()));
            this.txt_cod.setText(String.valueOf(entidad.getId()));
            this.txt_descripcion.setText(String.valueOf(entidad.getDescripcion()));
            this.txt_fecha.setDate(entidad.getFecha());
    }
    private void btn_buscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_buscarActionPerformed
        // TODO add your handling code here:
        
       BuscarMatricula ventana = new BuscarMatricula();
        ventana.enlazar(this);
        ventana.setVisible(true);
        ventana.requestFocus();
        
    }//GEN-LAST:event_btn_buscarActionPerformed

    private void txt_ciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txt_ciActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txt_ciActionPerformed

    private void btn_eliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_eliminarActionPerformed
        // TODO add your handling code here:
         try {
            String errorMessage = "";
            if (txt_cod.getText().isEmpty()) {
                errorMessage += "El sistema no ha generado el CODIGO de la Boleta de Pago.\n";
            }
            if (txt_ci.getText().isEmpty()) {
                errorMessage += "No ha ingresado el C.I. del interesado.\n";
            }
//            if (txt_detalle.getText().isEmpty()) {
//                errorMessage += "No ha especificado una DESCRIPCION para el pago.\n";
//            }

            if (errorMessage.equals("")) {
               // this.entidad= new EntidadPagoCredencial(text, WIDTH, errorMessage, monto, null, rootPaneCheckingEnabled);
                this.negocio.eliminacionLogica(txt_ci.getText(),txt_cod.getText());
                mostrarError("Se elimino Correctamente");
        } else {
            mostrarError(errorMessage);
            }
        } catch (Exception error) {
            mostrarError(error.getMessage());
        }
    }//GEN-LAST:event_btn_eliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PagoMatricula.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PagoMatricula.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PagoMatricula.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PagoMatricula.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new PagoMatricula().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_buscar;
    private javax.swing.JButton btn_eliminar;
    private javax.swing.JButton btn_nuevo;
    private javax.swing.JButton btn_pagar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JFormattedTextField txt_ci;
    private javax.swing.JFormattedTextField txt_cod;
    private javax.swing.JTextArea txt_descripcion;
    private com.toedter.calendar.JDateChooser txt_fecha;
    private javax.swing.JTextArea txt_mensaje;
    // End of variables declaration//GEN-END:variables



 
    @Override
    public void mostrarError(String mensaje) {
        this.txt_mensaje.setText(mensaje);
    }

    @Override
    public void cargarCredemcial(Object object) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    private void configurarFrame() {        
        if(this.Permiso.isAnadir()){
           this.btn_pagar.setEnabled(true);
           this.txt_ci.setEnabled(true);
           this.txt_cod.setEnabled(true);
           this.txt_fecha.setEnabled(true);
           this.txt_descripcion.setEnabled(true);
        }
        if(this.Permiso.isBuscar()){
            this.btn_buscar.setEnabled(true);
        }
        if(this.Permiso.isEditar()){
            
        }
        if(this.Permiso.isEliminar()){
           this.txt_ci.setEnabled(true);
           this.txt_cod.setEnabled(true);
           this.txt_fecha.setEnabled(true);
           this.btn_eliminar.setEnabled(true);
        }
    }

    private void setComponentesDisabled() {
        this.btn_buscar.setEnabled(false);
        this.btn_eliminar.setEnabled(false);
        this.btn_pagar.setEnabled(false);
        this.txt_ci.setEnabled(false);
        this.txt_cod.setEnabled(false);
        this.txt_descripcion.setEnabled(false);
        this.txt_fecha.setEnabled(false);
    }
}
